<?php
// @formatter:off
/**
 * @file c.edit.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-22 17:58:11 +0000 (Sat, 22 Jun 2013) $
 *
 * $Id: c.edit.php 83 2013-06-22 17:58:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/extensions/DirectoryOrganizer/controlers/c.edit.php $
 */
// @formatter:on

global $wcSession;
global $wcRule;
/**
 * @var DirectoryOrganizer
 */
global $wcRuleAction;

if($wcRule && $wcRuleAction && $wcProfile->allowedTo(WC_PERM_RULE_EDIT)) {
	$mode = $wcRuleAction->mode;

	if($_SERVER["REQUEST_METHOD"] == "POST") {
		global $wcDatabaseAccess;

		$wcRule->indexed(false);

		if($_POST["EDITACTION"] == "SET") {
			if($_POST["ACTION_NAME"] != $wcRuleAction->name()) {
				$wcRuleAction->setName($_POST["ACTION_NAME"]);
				$wcThemeAssigns["RULE-ACTION-NAME"] = $wcRuleAction->name();
			}
			if($_POST["ACTION_STATUS"] != $wcRuleAction->status) {
				$wcRuleAction->status = $_POST["ACTION_STATUS"];
			}
		}

		$db = WCConnection::Instance();
		$dbprefix = $wcDatabaseAccess['prefix'];

		$auxDir = dirname(__FILE__);
		$modeManager = "";
		switch($_POST["MODE"]) {
			case "MOVE":
			case "TAGGER":
			case "VMOVE":
				$modeManager = "{$auxDir}/c.edit.TAGGER-MOVE.php";
				break;
			case "CMOVE":
			case "COLLECTOR":
				$modeManager = "{$auxDir}/c.edit.COLLECTOR-CMOVE.php";
				break;
		}
		if(is_readable($modeManager)) {
			require_once $modeManager;
		}
	}

	$wcThemeAssigns["PATH"] = "Action Edit: '{$wcRuleAction->name()}' of rule '{$wcRule->name()}'";
	$wcThemeAssigns["DOR-URI"] = WC_E_DIRECTORY_ORGANIZER_URI;
	$wcThemeAssigns["mode-template"] = WC_E_DIRECTORY_ORGANIZER_TEMPLATES."/c.edit-{$mode}.html";
	$wcThemeAssigns["form-action"] = requestParams(true, $wcAction, array(), false);
	$wcThemeAssigns["action-status"] = $wcRuleAction->status;

	switch($mode) {
		case "MOVE":
		case "TAGGER":
		case "VMOVE":
			$wcThemeAssigns["mode"] = $mode;
			$wcThemeAssigns["mode-description"] = $mode;

			$aux = $wcRuleAction->directory();
			if($aux) {
				$wcThemeAssigns["directory"] = $aux->name;
			}

			$tags = array();
			foreach($wcRuleAction->tags() as $tag) {
				$tags[] = array(
					"id" => $tag->id(),
					"name" => $tag->name()
				);
			}
			$wcThemeAssigns["tags"] = $tags;
			if(count($tags)) {
				$wcThemeAssigns["tag"] = $tags[0]["name"];
			}

			break;

		case "CMOVE":
		case "COLLECTOR":
			$wcThemeAssigns["mode"] = $mode;
			$wcThemeAssigns["mode-description"] = $mode;

			$aux = $wcRuleAction->directory();
			if($aux) {
				$wcThemeAssigns["directory"] = $aux->name;
			}

			$aux = $wcRuleAction->collection();
			if($aux) {
				$wcThemeAssigns["collection"] = $aux->path();
			}

			break;

		default:
			$wcThemeAssigns["mode-description"] = $mode;
			unset($wcThemeAssigns["mode-template"]);
			break;
	}
} else {
	if(!$wcProfile->allowedTo(WC_PERM_RULE_EDIT)) {
		$wcThemeAssigns["error-message"] = "You don't have enough priviliges";
		require "{$this->_themeRoot}/controlers/contents/c.403.php";
	} else {
		$wcThemeAssigns["error-message"] = "There seems to be a problem with parameters";
		require "{$this->_themeRoot}/controlers/contents/c.500.php";
	}
}
?>